package com.zhuoyue.biz.core.api.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuoyue.biz.core.api.domain.*;
import com.zhuoyue.biz.core.api.dto.rep.SolVoiceSetVirtualDeviceRepDTO;
import com.zhuoyue.biz.core.api.service.GroupService;
import com.zhuoyue.biz.core.api.service.InfraredDeviceService;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuoyue.biz.core.api.dao.SetSolVoiceVirtualDeviceMapper;

/**
 * @author wjming
 */
@Service
public class SetSolVoiceVirtualDeviceService extends ServiceImpl<SetSolVoiceVirtualDeviceMapper, SetSolVoiceVirtualDevice> {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private InfraredDeviceService infraredDeviceService;

    public List<SolVoiceSetVirtualDeviceRepDTO> getList(String voiceId) {
        final String curtainGroup = "curtainGroup", dimmingLampGroup = "dimmingLampGroup", intelligentLampGroup = "intelligentLampGroup",
                acController = "acController", curtain = "curtain", dimmingLamp = "dimmingLamp", intelligentLamp = "intelligentLamp",
                acPanel = "acPanel";

        List<SetSolVoiceVirtualDevice> list = list(Wrappers.<SetSolVoiceVirtualDevice>lambdaQuery()
                .eq(SetSolVoiceVirtualDevice::getVoiceId, voiceId));
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<SetSolVoiceVirtualDevice> groupIdList = list.stream().filter(s -> s.getVirtualType().equals(curtainGroup) ||
                s.getVirtualType().equals(dimmingLampGroup) || s.getVirtualType().equals(intelligentLampGroup)).collect(Collectors.toList());

        List<SetSolVoiceVirtualDevice> acIdList = list.stream().filter(s -> s.getVirtualType().equals(acController)).collect(Collectors.toList());

        List<SetSolVoiceVirtualDevice> deviceList = list.stream().filter(s -> s.getVirtualType().equals(curtain) || s.getVirtualType().equals(dimmingLamp) ||
                s.getVirtualType().equals(intelligentLamp) || s.getVirtualType().equals(acPanel)).collect(Collectors.toList());

        List<SolVoiceSetVirtualDeviceRepDTO> solVoiceSetVirtualDeviceRepDTOList = new ArrayList<>(6);

        if (CollectionUtils.isNotEmpty(groupIdList)) {
            groupIdList.forEach(l -> {
                SolVoiceSetVirtualDeviceRepDTO solVoiceSetVirtualDeviceRep = new SolVoiceSetVirtualDeviceRepDTO().doToDto(l);
                Group groupDo = groupService.getById(l.getEndDeviceId());
                if (groupDo != null) {
                    solVoiceSetVirtualDeviceRep.setVirtualName(groupDo.getName());
                }
                solVoiceSetVirtualDeviceRepDTOList.add(solVoiceSetVirtualDeviceRep);
            });
        }
        if (CollectionUtils.isNotEmpty(acIdList)) {
            acIdList.forEach(l -> {
                SolVoiceSetVirtualDeviceRepDTO solVoiceSetVirtualDeviceRep = new SolVoiceSetVirtualDeviceRepDTO().doToDto(l);
                InfraredDeviceDO device = infraredDeviceService.getOne(Wrappers.<InfraredDeviceDO>lambdaQuery()
                        .eq(InfraredDeviceDO::getDeviceId, l.getEndDeviceId())
                        .eq(InfraredDeviceDO::getCentralAcCode, l.getAddress()));
                if (device != null) {
                    solVoiceSetVirtualDeviceRep.setVirtualName(device.getName());
                }
                solVoiceSetVirtualDeviceRepDTOList.add(solVoiceSetVirtualDeviceRep);
            });
        }

        if (CollectionUtils.isNotEmpty(deviceList)) {
            deviceList.forEach(l -> {
                SolVoiceSetVirtualDeviceRepDTO solVoiceSetVirtualDeviceRep = new SolVoiceSetVirtualDeviceRepDTO().doToDto(l);
                DeviceDO device = deviceService.getById(l.getEndDeviceId());
                if (device != null) {
                    solVoiceSetVirtualDeviceRep.setVirtualName(device.getName());
                }
                solVoiceSetVirtualDeviceRepDTOList.add(solVoiceSetVirtualDeviceRep);
            });
        }

        return solVoiceSetVirtualDeviceRepDTOList;
    }

    /**
     * 添加虚拟设备信息
     *
     * @param setSolVoiceVirtualDevice
     * @return
     */
    public SolVoiceSetVirtualDeviceRepDTO addVirtualDevice(SetSolVoiceVirtualDevice setSolVoiceVirtualDevice) {
        if (StringUtils.isEmpty(setSolVoiceVirtualDevice.getVirtualType())) {
            setSolVoiceVirtualDevice.setAddress("01");
        }
        boolean save = save(setSolVoiceVirtualDevice);
        if (!save) {
            throw new ZyServerException(ErrorType.Service_Fail);
        }
        SetSolVoiceVirtualDevice byId = getById(setSolVoiceVirtualDevice.getId());
        return new SolVoiceSetVirtualDeviceRepDTO().doToDto(byId);
    }


    public void verifyThatTheAddedDeviceIs(SetSolVoiceVirtualDevice setSolVoiceVirtualDevice) {
        String group = "group";
        String virtualType = setSolVoiceVirtualDevice.getVirtualType();
        if (!StringUtils.containsIgnoreCase(virtualType, group)) {
            int count = count(Wrappers.<SetSolVoiceVirtualDevice>lambdaQuery()
                    .eq(SetSolVoiceVirtualDevice::getVoiceId, setSolVoiceVirtualDevice.getVoiceId())
                    .eq(SetSolVoiceVirtualDevice::getGatewayId, setSolVoiceVirtualDevice.getGatewayId()).and(i -> i.eq(SetSolVoiceVirtualDevice::getVirtualType, setSolVoiceVirtualDevice.getVirtualType())
                            .or().eq(SetSolVoiceVirtualDevice::getVirtualType, setSolVoiceVirtualDevice.getVirtualType() + "Group")));
            if (count > 0) {
                throw new ZyServerException(ErrorType.Service_Fail.getCode(), "数据已存在");
            }
        }
        if (StringUtils.containsIgnoreCase(virtualType, group)) {
            String group1 = StringUtils.removeEndIgnoreCase(setSolVoiceVirtualDevice.getVirtualType(), "Group");
            int count = count(Wrappers.<SetSolVoiceVirtualDevice>lambdaQuery()
                    .eq(SetSolVoiceVirtualDevice::getVoiceId, setSolVoiceVirtualDevice.getVoiceId())
                    .eq(SetSolVoiceVirtualDevice::getGatewayId, setSolVoiceVirtualDevice.getGatewayId()).and(i -> i.eq(SetSolVoiceVirtualDevice::getVirtualType, setSolVoiceVirtualDevice.getVirtualType()).or().eq(SetSolVoiceVirtualDevice::getVirtualType, group1)));
            if (count > 0) {
                throw new ZyServerException(ErrorType.Service_Fail.getCode(), "数据已存在");
            }
        }
    }

    /**
     * 修改虚拟设备信息
     *
     * @param setSolVoiceVirtualDevice
     * @return
     */
    public SolVoiceSetVirtualDeviceRepDTO putVoiceVirtualDeviceRelation(SetSolVoiceVirtualDevice setSolVoiceVirtualDevice) {
        if (StringUtils.isEmpty(setSolVoiceVirtualDevice.getVirtualType())) {
            setSolVoiceVirtualDevice.setAddress("01");
        }
        boolean b = updateById(setSolVoiceVirtualDevice);
        if (!b) {
            throw new ZyServerException(ErrorType.Service_Fail);
        }
        SetSolVoiceVirtualDevice byId = getById(setSolVoiceVirtualDevice.getId());
        return new SolVoiceSetVirtualDeviceRepDTO().doToDto(byId);
    }
}





